package com.beiming.libra.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.beiming.libra.domain.dto.requestdto.EmailRequestDTO;
import com.beiming.libra.domain.dto.requestdto.LoginRequestDTO;
import com.beiming.libra.domain.dto.requestdto.MobilePhoneRequestDTO;
import com.beiming.libra.domain.dto.requestdto.NameVerifyRequestDTO;
import com.beiming.libra.domain.dto.requestdto.RefreshTokenRequestDTO;
import com.beiming.libra.domain.dto.requestdto.SMSLoginRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRegisterCompRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRegisterInfoRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRegisterRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserResetPwdRequestDTO;
import com.beiming.libra.domain.dto.responsedto.LoginInfoResponseDTO;
import com.beiming.libra.domain.dto.responsedto.LoginTokenResponseDTO;
import com.beiming.libra.domain.dto.responsedto.RefreshTokenResponseDTO;
import com.beiming.libra.helper.Result;
import com.beiming.libra.security.TokenGenerator;
import com.beiming.libra.service.PersonalService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "登录注册模块", tags = "登录注册模块")
@RequestMapping("/auth")
public class AuthController {

  @Resource
  private PersonalService personalService;
  @Resource
  private TokenGenerator tokenGenerator;

  /**
   * 注册(个人)
   */
  @ApiOperation(value = "注册(个人)", notes = "注册(个人)")
  @RequestMapping(value = "register", method = RequestMethod.POST)
  @ResponseBody
  public Result register(@RequestBody @Valid UserRegisterRequestDTO dto) {
    personalService.register(dto);
    return Result.success();
  }

  /**
   * 个人注册，完善个人信息
   */
  @ApiOperation(value = "个人注册，完善信息", notes = "个人注册，完善信息")
  @RequestMapping(value = "next", method = RequestMethod.POST)
  @ResponseBody
  public Result next(@RequestBody @Valid UserRegisterInfoRequestDTO dto) {
    personalService.next(dto);
    return Result.success();
  }
  /**
   * 注册（企业）
   */
  @ApiOperation(value = "注册（企业）", notes = "注册（企业）")
  @RequestMapping(value = "register/company", method = RequestMethod.POST)
  @ResponseBody
  public Result registerForCompany(@RequestBody @Valid UserRegisterCompRequestDTO dto) {
    personalService.registerForComp(dto);
    return Result.success();
  }

  /**
   * 企业注册，完善信息
   */
  @ApiOperation(value = "企业注册，完善信息", notes = "企业注册，完善信息")
  @RequestMapping(value = "next/company", method = RequestMethod.POST)
  @ResponseBody
  public Result nextForCompany(@RequestBody @Valid UserRegisterInfoRequestDTO dto) {
    personalService.next(dto);
    return Result.success();
  }

  /**
   * 注册实名认证（个人）
   */
  @ApiOperation(value = "注册实名认证（个人）", notes = "注册实名认证（个人）")
  @RequestMapping(value = "realNameVerify", method = RequestMethod.POST)
  @ResponseBody
  public Result realNameVerify(@RequestBody @Valid NameVerifyRequestDTO dto) {
    personalService.realNameVerify(dto);
    return Result.success();
  }


  /**
   * 短信验证码登录
   */
  @ApiOperation(value = "短信验证码登录", notes = "短信验证码登录")
  @RequestMapping(value = "smsLogin", method = RequestMethod.POST)
  @ResponseBody
  public Result<LoginTokenResponseDTO> smsLogin(@RequestBody @Valid SMSLoginRequestDTO dto) {
    LoginInfoResponseDTO loginInfo = personalService.smsLogin(dto);
    LoginTokenResponseDTO res = tokenGenerator.generateLoginToken(loginInfo);
    return Result.success(res);
  }

  /**
   * 用户名密码登录
   */
  @ApiOperation(value = "用户名密码登录", notes = "用户名密码登录")
  @RequestMapping(value = "login", method = RequestMethod.POST)
  @ResponseBody
  public Result<LoginTokenResponseDTO> login(@RequestBody @Valid LoginRequestDTO dto) {
    LoginInfoResponseDTO loginInfo = personalService.login(dto);
    LoginTokenResponseDTO res = tokenGenerator.generateLoginToken(loginInfo);
    return Result.success(res);
  }

  /**
   * 刷新token
   */
  @ApiOperation(value = "刷新token", notes = "刷新token")
  @RequestMapping(value = "refreshToken", method = RequestMethod.POST)
  @ResponseBody
  public Result<RefreshTokenResponseDTO> refreshToken(@RequestBody @Valid RefreshTokenRequestDTO dto) {
    RefreshTokenResponseDTO res = tokenGenerator.refreshToken(dto.getRefreshToken());
    return Result.success(res);
  }

  /**
   * 获取短信验证码（已注册用户）
   */
  @ApiOperation(value = "获取短信验证码（已注册用户）", notes = "获取短信验证码（已注册用户）")
  @RequestMapping(value = "getSMSCode", method = RequestMethod.POST)
  @ResponseBody
  public Result getSMSCode(@RequestBody @Valid MobilePhoneRequestDTO dto) {
    personalService.getSMSCode(dto);
    return Result.success();
  }

  /**
   * 获取短信验证码（未注册用户）
   */
  @ApiOperation(value = "获取短信验证码（未注册用户）", notes = "获取短信验证码（未注册用户）")
  @RequestMapping(value = "getPhoneCode", method = RequestMethod.POST)
  @ResponseBody
  public Result getPhoneCode(@RequestBody @Valid MobilePhoneRequestDTO dto) {
    personalService.getPhoneCode(dto);
    return Result.success();
  }

  /**
   * 获取邮件验证码
   */
  @ApiOperation(value = "获取邮件验证码", notes = "获取邮件验证码")
  @RequestMapping(value = "getMailCode", method = RequestMethod.POST)
  @ResponseBody
  public Result getMailCode(@RequestBody @Valid EmailRequestDTO dto) {
    personalService.getEmailCode(dto);
    return Result.success();
  }
  
  /**
   * 重置密码
   */
  @ApiOperation(value = "重置密码", notes = "重置密码")
  @RequestMapping(value = "resetPwd", method = RequestMethod.POST)
  @ResponseBody
  public Result resetPwd(@RequestBody @Valid UserResetPwdRequestDTO dto) {
    personalService.resetPwd(dto);
    return Result.success();
  }
  

}
